﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace NinjaToolbox.PhysicsSystem
{
    public class BoxShapeSceneHandleDrawer : ShapeSceneHandleDrawerBase
    {
        BoxShape mBoxShape;


        public override void Initialization(SerializedObject serializeObject, IPhysicsShape shape)
        {
            mBoxShape = shape as BoxShape;

            base.Initialization(serializeObject, shape);
        }

        protected override void DrawSceneGUI(SceneView sceneView)
        {
            DrawHandleButtons();
        }

        void DrawHandleButtons()
        {
            if (Camera.current == null) return;

            var cacheColor = Handles.color;

            var matrix = mBoxShape.matrix;

            var right = matrix.rotation * Vector3.right;
            var up = matrix.rotation * Vector3.up;
            var forward = matrix.rotation * Vector3.forward;

            var cameraForward = Camera.current.transform.forward;

            var handleSizeScale = 0.035f;
            var handleMaxSize = 0.1f;
            var defaultGizmosColor = PhysicsUtil.PHYSICS_SHAPE_GIZMOS_COLOR;

            var minPosition = matrix.MultiplyPoint3x4(mBoxShape.offset - mBoxShape.size);
            var maxPosition = matrix.MultiplyPoint3x4(mBoxShape.offset + mBoxShape.size);

            using (var changeCheck = new EditorGUI.ChangeCheckScope())
            {
                var point = matrix.MultiplyPoint3x4(mBoxShape.offset + Vector3.right * mBoxShape.size.x * 0.5f);
                Handles.color = Vector3.Dot(cameraForward, right) < 0 ? defaultGizmosColor : new Color(defaultGizmosColor.r, defaultGizmosColor.g, defaultGizmosColor.b, 0.5f);
                var handleSize = Mathf.Min(HandleUtility.GetHandleSize(point) * handleSizeScale, handleMaxSize);
                var newPoint = Handles.FreeMoveHandle(GUIUtility.GetControlID(FocusType.Passive), point, Quaternion.identity, handleSize, Vector3.zero, Handles.DotHandleCap);//+x
                maxPosition.x = matrix.inverse.MultiplyPoint3x4(newPoint).x;

                point = matrix.MultiplyPoint3x4(mBoxShape.offset - Vector3.right * mBoxShape.size.x * 0.5f);
                Handles.color = Vector3.Dot(cameraForward, -right) < 0 ? defaultGizmosColor : new Color(defaultGizmosColor.r, defaultGizmosColor.g, defaultGizmosColor.b, 0.5f);
                handleSize = Mathf.Min(HandleUtility.GetHandleSize(point) * handleSizeScale, handleMaxSize);
                newPoint = Handles.FreeMoveHandle(GUIUtility.GetControlID(FocusType.Passive), point, Quaternion.identity, handleSize, Vector3.zero, Handles.DotHandleCap);//-x
                minPosition.x = matrix.inverse.MultiplyPoint3x4(newPoint).x;

                point = matrix.MultiplyPoint3x4(mBoxShape.offset + Vector3.up * mBoxShape.size.y * 0.5f);
                Handles.color = Vector3.Dot(cameraForward, up) < 0 ? defaultGizmosColor : new Color(defaultGizmosColor.r, defaultGizmosColor.g, defaultGizmosColor.b, 0.5f);
                handleSize = Mathf.Min(HandleUtility.GetHandleSize(point) * handleSizeScale, handleMaxSize);
                newPoint = Handles.FreeMoveHandle(GUIUtility.GetControlID(FocusType.Passive), point, Quaternion.identity, handleSize, Vector3.zero, Handles.DotHandleCap);//+y
                maxPosition.y = matrix.inverse.MultiplyPoint3x4(newPoint).y;

                point = matrix.MultiplyPoint3x4(mBoxShape.offset - Vector3.up * mBoxShape.size.y * 0.5f);
                Handles.color = Vector3.Dot(cameraForward, -up) < 0 ? defaultGizmosColor : new Color(defaultGizmosColor.r, defaultGizmosColor.g, defaultGizmosColor.b, 0.5f);
                handleSize = Mathf.Min(HandleUtility.GetHandleSize(point) * handleSizeScale, handleMaxSize);
                newPoint = Handles.FreeMoveHandle(GUIUtility.GetControlID(FocusType.Passive), point, Quaternion.identity, handleSize, Vector3.zero, Handles.DotHandleCap);//-y
                minPosition.y = matrix.inverse.MultiplyPoint3x4(newPoint).y;

                point = matrix.MultiplyPoint3x4(mBoxShape.offset + Vector3.forward * mBoxShape.size.z * 0.5f);
                Handles.color = Vector3.Dot(cameraForward, forward) < 0 ? defaultGizmosColor : new Color(defaultGizmosColor.r, defaultGizmosColor.g, defaultGizmosColor.b, 0.5f);
                handleSize = Mathf.Min(HandleUtility.GetHandleSize(point) * handleSizeScale, handleMaxSize);
                newPoint = Handles.FreeMoveHandle(GUIUtility.GetControlID(FocusType.Passive), point, Quaternion.identity, handleSize, Vector3.zero, Handles.DotHandleCap);//+z
                maxPosition.z = matrix.inverse.MultiplyPoint3x4(newPoint).z;

                point = matrix.MultiplyPoint3x4(mBoxShape.offset - Vector3.forward * mBoxShape.size.z * 0.5f);
                Handles.color = Vector3.Dot(cameraForward, -forward) < 0 ? defaultGizmosColor : new Color(defaultGizmosColor.r, defaultGizmosColor.g, defaultGizmosColor.b, 0.5f);
                handleSize = Mathf.Min(HandleUtility.GetHandleSize(point) * handleSizeScale, handleMaxSize);
                newPoint = Handles.FreeMoveHandle(GUIUtility.GetControlID(FocusType.Passive), point, Quaternion.identity, handleSize, Vector3.zero, Handles.DotHandleCap);//-z
                minPosition.z = matrix.inverse.MultiplyPoint3x4(newPoint).z;

                if (changeCheck.changed)
                {
                    Undo.RecordObject(mSerializeObject.targetObject, "NinjaToolBox.PhysicsSystem.BoxShape");

                    mBoxShape.offset = (maxPosition + minPosition) * 0.5f;
                    mBoxShape.size = (maxPosition - minPosition);
                }
                Handles.color = cacheColor;
            }
        }
    }
}
